package io.a;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        DruidPlugin druidPlugin = new DruidPlugin("jdbc:mysql://47.98.45.56/sql?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&allowPublicKeyRetrieval=true&useTimezone=true&serverTimezone=GMT%2B8", "root", "inspection@");
        druidPlugin.start();

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
//        arp.addSqlTemplate("all.sql");
        arp.setDialect(new MysqlDialect());
        arp.setDevMode(true);// 开发模式

        Engine engine = arp.getEngine();
//        Engine engine = Engine.use();
        engine.setDevMode(true); // 热加载
        engine.setToClassPathSourceFactory();// 加载路径
//        Engine.setChineseExpression(true); // 支持中文表达式
        engine.setDatePattern("yyyy-MM-dd HH:mm:ss");// 设置#date()格式

        engine.addSharedMethod(StrKit.class);
        engine.addSharedObject("author", "eric");
//        engine.addSharedFunction(""); // #define test()

        arp.start();

        String boundSql = getBoundSql(engine);
        System.out.println(boundSql);

        List<Record> list = Db.find(boundSql);// 执行SQL
        List<Record> list2 = list.stream().map(r -> {// -> 驼峰式
            Record record = new Record();

            String[] columnNames = r.getColumnNames();
            for (String columnName : columnNames) {
                record.set(StrKit.toCamelCase(columnName), r.get(columnName));
            }
            return record;
        }).toList();
        System.out.println(list);
        System.out.println(list2);
    }

    private static String getBoundSql(Engine engine) {
        /*
        一、简单粗暴，使用enjoy直接处理 <- 目前使用这个，够用
        二、分为两个阶段
        1.enjoy做转换
        2.replace做替换
         */
        String sqlTemplate = """
				SELECT * FROM test_user WHERE 1=1 
				
				### if语句用法
				#if(startTime != null && endTime != null)
				    AND create_time BETWEEN '#(startTime)' AND '#(endTime)'
				#end
				
				#if(list && notEmpty(list))
				    AND name in (
                        ### for语句用法
                        #for(x:list)
                            '#(x)'
                            #if((for.count) != (for.size))
                                , 
                            #end
                        #end
				 )
				#end
				""";
        Kv para = Kv.by("id", 1).set("startTime", LocalDateTime.now().minusMonths(1)).set("endTime", LocalDateTime.now()).set("list", Arrays.asList("mali", "mary"));
        return engine.getTemplateByString(sqlTemplate).renderToString(para);
    }
}
